<%@page contentType="text/html;charset=utf-8" pageEncoding="UTF-8" %>
<%@page import="org.ipo.mm.web.util.pager.AbstractPager, org.ipo.mm.web.util.ConstantManager, java.util.Collection, org.ipo.mm.web.util.security.SecurityManager, java.util.Vector" %>
<%@taglib prefix="l" uri="http://struts.apache.org/tags-logic" %>
<%@taglib prefix="b" uri="http://struts.apache.org/tags-bean" %>
<%@taglib prefix="h" uri="http://struts.apache.org/tags-html"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%String readOnlyStr = (String) session.getAttribute("readOnly");%>

<!--
<%=request.getContextPath()%>
<%=request.getRequestURL()%>
-->


<h1>
    <% if (readOnlyStr.equals("true")) {%>
    <b:message key="institutions_show_read_mode"/>
    <%} else {%>
    <b:message key="institutions_show_edit_mode"/>
    <%}%>
</h1>
<div id="createNewInstitution" class ="displayNone">
    <%
                Collection<Integer> privileges = (Collection<Integer>) request.getSession().getAttribute(ConstantManager.SESSION_USER_PRIVILEGES);
                if (privileges.contains(SecurityManager.CREATE_INSTITUTION_PRIVILEGE)) {%>

    <h:link styleClass="createNew" action="/protected/dispatch?method=showCreateInstForm"><b:message key="inst_create"/></h:link>

    <%}%>
</div>
<div>
    <table id="institutionListTable" class="display sortableTable">
        <thead>
            <tr>
                <th><b:message key="inst_name"/></th>
                <th><b:message key="inst_type"/></th>
                <th><b:message key="inst_family"/></th>
                <th><b:message key="inst_region"/></th>
                <th><b:message key="inst_city"/></th>
                <th class="do-not-sort"><b:message key="menu_representatives"/></th>
                <th class="do-not-sort"><b:message key="meetings_meetings"/></th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th><input type="text" class="search_init" /></th>
                <th><input type="text" class="search_init" /></th>
                <th><input type="text" class="search_init" /></th>
                <th><input type="text"  class="search_init" /></th>
                <th><input type="text"  class="search_init" /></th>
                <th><input type="text"  class="search_init" /></th>
                <th></th>
            </tr>
        </tfoot>
        <tbody>
            <l:iterate id="inst" name="INST_SEARCH_RESULTS" scope="request">
                <tr>
                    <td>
                        <% if (readOnlyStr.equals("false")) {%>
                        <p class="link"
                           onclick="getHtmlAjax('<%= request.getContextPath()%>/protected/dispatch.do?method=showUpdateInstForm&iid=${inst.id}','#institutionDialog','#institutionForm');">
                            <b:write name="inst" property="name"/>
                        </p>
                        <%} else {%>
                        <p class="link"
                           onclick="getHtmlAjax('<%= request.getContextPath()%>/protected/dispatch.do?method=viewInstForm&iid=${inst.id}','#institutionDialog','#institutionForm');">
                            <b:write name="inst" property="name"/>
                        </p>
                        <%}%>
                    </td>
                    <td><span>${inst.type.name}</span></td>
                    <td><span>${inst.family.name}</span></td>
                    <td><span>${inst.region.name}</span></td>
                    <td><span>${inst.city.name}</span></td>
                    <td>
                        <c:forEach var="rep" items="${inst.representativeCollection}" varStatus="rowCounter">
                            <p id="reprDetail${rep.id}" class="link" onclick="getHtmlAjax('/IPOMeetingManager-web/protected/dispatch.do?method=showUpdateReprForm&rid=${rep.id}&clearStack=true','#reprDialog','#reprForm');">
                                <c:out value="${rowCounter.count}" />. ${rep.firstName} ${rep.lastName}</p>
                            </c:forEach>


                    </td>
                    <td>
                        <h:link action="/protected/meeting_search">
                            <h:param name="iid"  value="${inst.id}"/>
                            <h:param name="readOnly"  value="true"/>
                            <b:message key="inst_find_meetings"/>
                        </h:link>
                    </td>

                </tr>
            </l:iterate>
        <tbody>

    </table>
</div>

<div id="institutionDialog" class="dialog" title="">
</div>
<div id ="messageregion"></div>
<div id ="reprDialog" class="dialog"></div>

<script type="text/javascript">
    function cancelForm(){
        $(".dialog").dialog('close');
    }
</script>